#parse('include.java')
#set($sp = $CSU.getSuperPackage($model.package,1))
#set($TYPE = $model.name)
#set($type = $CSU.getVariable($TYPE))
#set($serviceName = $type+'Service')
#if($sp)
package ${sp}.action;
#end
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.page.Page;

import $!{model.package}.${TYPE};
import ${sp}.service.${TYPE}Service;
import ${sp}.action.BasicBizAction;

/**
 * $!{modelLabel}操作xhtml
 * @author wangjg
 */
@Controller
public class ${TYPE}Action extends BasicBizAction{
	@Autowired
	private ${TYPE}Service ${serviceName};
	
	/**
	 * $!{modelLabel}列表
	 * @param keyword 查询关键字
	 */
	@RequestMapping("/${type}/list.xhtml")
	public String list(ModelMap model#allParams($model)){
		Page page = this.getPage(20);
		List<${TYPE}> list = ${type}Service.pageQuery(page#allParamVals($model));
		model.put("${type}List", list);
		model.put("page", page);

		this.setEnumData(model#allParamVals($model));

		return "/${type}/list.vm";
	}
	
	private void setEnumData(Map<String,Object> model#allParams($model)){
#trim('eachrow', 2, 'rn')
		#foreach($field in $model.fields)
			#if($field.reference)
				#set($ref = $field.reference)
				#set($refModel = $CONTEXT.findModel($ref.modelName))
				#if($MU.isStaticRef($ref))
					model.put("${refModel.name}", ${refModel.name}.map);
				#else
					#set($vRefName = $CSU.getVariable($refModel.name))
					List<$refModel.name> ${vRefName}List = ${vRefName}Service.get${refModel.name}Enum();
					model.put("${vRefName}Options", ${vRefName}List);
				#end
			#end
		#end
#end
	}

	/**
	 * 编辑$!{modelLabel}
	 * @param id 编号
	 */
	@RequestMapping("/${type}/edit.xhtml")
	public String edit(ModelMap model, Long id){
		if(id!=null){
			${TYPE} ${type} = this.${serviceName}.get${TYPE}(id);
			this.checkOwner(${type});
			model.put("${type}", ${type});	
			model.put("_update", "Y");
		}
		this.setEnumData(model#allParamVals($model));
		return "/${type}/edit.vm"; 
	}
	
	/**
	 * 保存$!{modelLabel}
	 */
	@RequestMapping("/${type}/save.xhtml")
	public String save(String _update, Long id){
		if(!"Y".equals(_update)){
			${TYPE} ${type} = new ${TYPE}();
			this.bindParams(${type});
			this.setOwner(${type});
			this.${type}Service.save(${type});
		}else{
			${TYPE} ${type} = this.${type}Service.get${TYPE}(id);
			this.checkOwner(${type});
			this.bindParams(${type});
			this.${type}Service.update(${type});
		}
		return "redirect:/${type}/list.xhtml";
	}
	
	/**
	 * 删除$!{modelLabel}
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/${type}/delete.xhtml")
	public String delete(Long id){
		${TYPE} ${type} = this.${serviceName}.get${TYPE}(id);
		this.checkOwner(${type});
		this.${serviceName}.delete(${type});
		return "redirect:/${type}/list.xhtml";
	}

}
